//
//  aRNetworkRequest.m
//  taobaos
//
//  Created by 陈柱成 on 16/6/27.
//  Copyright © 2016年 aReu. All rights reserved.
//

#import "aRNetworkRequest.h"
#import "MBProgressHUD.h"
#import "AFNetworking.h"

@interface aRNetworkRequest ()
{
    NSURLSessionDownloadTask *_downloadTask;
}
@end

@implementation aRNetworkRequest

+(instancetype)shareInstance{
    static aRNetworkRequest *aRequest = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        aRequest = [[aRNetworkRequest alloc] init];
    });
    return aRequest;
}

-(void)requestDataToEachPageSetting:(aRCallback)arcb
                 byRequestUrlString:(NSString*)urlString
{
    __block MBProgressHUD *hud = nil;
    dispatch_async(dispatch_get_main_queue(), ^{
        hud = [MBProgressHUD showHUDAddedTo:aRAPPLICATION.keyWindow animated:YES];
        hud.label.text = NSLocalizedString(@"正在加载中...", @"HUD loading title");
        hud.label.font = [UIFont italicSystemFontOfSize:12.f];
    });
    AFHTTPSessionManager *session                     = [AFHTTPSessionManager manager];
    NSMutableSet *acceptableSet                       = [NSMutableSet setWithSet:session.responseSerializer.acceptableContentTypes];
    [acceptableSet addObject:@"text/html"];
    session.responseSerializer.acceptableContentTypes = acceptableSet;
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [session GET:urlString
              parameters:nil
                progress:^(NSProgress * _Nonnull downloadProgress) {
                    debugLog(@"加载进度:%lld",downloadProgress.completedUnitCount);
                }
                 success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                     dispatch_async(dispatch_get_main_queue(), ^{
                         [hud hideAnimated:YES];
                         !responseObject ? : arcb(responseObject,nil);
                     });
                 }
                 failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                     [hud hideAnimated:YES];
                     arcb(nil,error);
                 }];
        });
}

-(void)requestByPOSTWithDic:(NSDictionary*)dic
         byRequestUrlString:(NSString*)urlString
                           :(aRCallback)arcb
{
    __block MBProgressHUD *hud = nil;
    dispatch_async(dispatch_get_main_queue(), ^{
        hud = [MBProgressHUD showHUDAddedTo:aRAPPLICATION.keyWindow animated:YES];
        hud.label.text = NSLocalizedString(@"正在加载中...", @"HUD loading title");
        hud.label.font = [UIFont italicSystemFontOfSize:12.f];
    });
    AFHTTPSessionManager *session                     = [AFHTTPSessionManager manager];
    NSMutableSet *acceptableSet                       = [NSMutableSet setWithSet:session.responseSerializer.acceptableContentTypes];
    [acceptableSet addObject:@"text/html"];
    session.responseSerializer.acceptableContentTypes = acceptableSet;
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [session POST:urlString parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
            debugLog(@"post:%lld",uploadProgress.completedUnitCount);
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [hud hideAnimated:YES];
                !responseObject ? : arcb(responseObject,nil);
            });
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [hud hideAnimated:YES];
            arcb(nil,error);
        }];
    });
}



@end
